package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckGroupDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service(interfaceClass = CheckGroupService.class)
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {
    @Autowired
    private  CheckGroupDao checkGroupDao;

    //添加检查组合，同时需要设置检查组合和检查项的关联关系
    @Override
    public void add(CheckGroup checkGroup, Integer[] checkitemIds) {
        checkGroupDao.add(checkGroup);
        setCheckGroupAndCheckItem(checkGroup.getId(),checkitemIds);
    }
    //设置关联表
    private void setCheckGroupAndCheckItem(Integer id, Integer[] checkitemIds) {
        if (checkitemIds!=null&&checkitemIds.length!=0){
            for (Integer checkId:checkitemIds){
                Map<String, Integer> map = new HashMap<>();
                map.put("checkgroup_id",id);
                map.put("checkitem_id",checkId);
                checkGroupDao.setCheckGroupAndCheckItem(map);
            }
        }
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //基于mybatis框架提供的分页插件助手完成
        if (queryPageBean.getQueryString()!=null&&queryPageBean.getQueryString().length()!=0){
            PageHelper.startPage(1,10);
        }else{
            PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        }
    //    PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<CheckGroup> page = checkGroupDao.selectByCondition(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public CheckGroup findById(Integer id) {
        return checkGroupDao.findById(id);

    }

    @Override
    public List<Integer> findCheckItemIdsByCheckGroupId(Integer id) {
       return checkGroupDao.findCheckItemIdsByCheckGroupId(id);
    }

    @Override
    public void edit(CheckGroup checkGroup, Integer[] checkitemIds) {
        //删除原有关系
        checkGroupDao.deleteAssociation(checkGroup.getId());
        //向中间表添加关联
        if(checkitemIds != null && checkitemIds.length > 0) {
            for (Integer id : checkitemIds) {
                Map<String,Integer> map = new HashMap<>();
                map.put("checkgroup_id",checkGroup.getId());
                map.put("checkitem_id",id);
                checkGroupDao.setCheckGroupAndCheckItem(map);
            }
        }
        //向检查组表添加数据
        checkGroupDao.edit(checkGroup);
    }

    @Override
    public void delete(Integer id) {
        //检查该组和套餐是否有关联关系
        long count=checkGroupDao.findTaoCanCountByCheckGroupId(id);
        if (count>0){
            //当前检查项被引用，不能删除
            throw new RuntimeException("当前检查项被引用，不能删除");
        }
        //删除中间表
        checkGroupDao.deleteAssociation(id);
        //删除检查组中信息
        checkGroupDao.delete(id);
    }

    @Override
    public List<CheckGroup> findAll() {
        return checkGroupDao.findAll();
    }
}
